package com.dd.shop4j.common.utils.interceptor;

import java.util.Date;

import org.springframework.core.convert.converter.Converter;
import org.springframework.util.StringUtils;

import com.dd.shop4j.common.utils.DateUtils;

/**
 * Spring MVC日期转换器
 * @author Administrator
 *
 */
public class DateConverter implements Converter<String, Date> {

	@Override
	public Date convert(String source) {
		if (StringUtils.hasText(source)) {
			if (source.indexOf(":") == -1 && source.length() == 10) {
				return DateUtils.parseYmd(source);
			} else if (source.indexOf(":") > 0 && source.length() == 16) {
				return DateUtils.parseYmdHmi(source);
			} else if (source.indexOf(":") > 0 && source.length() == 19) {
				return DateUtils.parseYmdHmis(source);
			} else if (source.indexOf(":") > 0 && source.length() == 21) {
				return convert(source.replace(".0", ""));
			} else {
				throw new IllegalArgumentException("不能转换时间，时间格式错误。");
			}
		}
		return null;
	}

}
